package cn.puge.usercenter.controller;


import cn.puge.usercenter.entity.PugeNewinfo;
import cn.puge.usercenter.service.PugeNewinfoService;
import cn.puge.usercenter.utils.ExcelUtil;
import com.alibaba.excel.EasyExcel;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author LIJW
 * @since 2020-12-03
 */
@RestController
@RequestMapping("/usercenter/puge-newinfo")
public class PugeNewinfoController {


    @Autowired
    private PugeNewinfoService pugeNewinfoService;

    //循环设置要添加的数据，最终封装到list集合中
    @GetMapping("test")
    private void data(HttpServletResponse response) throws IOException {

        List<PugeNewinfo> list1 = pugeNewinfoService.list(null);
//        System.out.println(list1);
//        List<PugeNewinfo> list = new ArrayList<PugeNewinfo>();
//
//        for (PugeNewinfo pugeNewinfo : list1) {
//            PugeNewinfo pugeNewinfo1 = new PugeNewinfo();
//            BeanUtils.copyProperties(pugeNewinfo,pugeNewinfo1);
//            list.add(pugeNewinfo1);
//        }
        // 这里注意 有同学反应使用swagger 会导致各种问题，请直接用浏览器或者用postman
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
        String fileName = URLEncoder.encode("测试", "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
        EasyExcel.write(response.getOutputStream(), PugeNewinfo.class).sheet("模板").doWrite(list1);
    }


}

